#include "parametersettings.h"
#include "ui_parametersettings.h"
#include "settings/itemwithcbx.h"
#include "settings/itemwithlabel.h"
#include <QLayout>
#include <QDebug>
//#include <TSettings>
#include "common/tsettings/tsettings.h"
#include "common/gui/input/numberkeyboard.h"
#include "common/gui/mymessagedialog.h"
#include "mainwidget.h"
#include "comsettingdlg.h"
#include "serversetting.h"
#include "networkSettings/ipsetting.h"
#include "common/touch_calibration.h"
#include "common/touch_calibration_nine.h"
#include <QDesktopWidget>



//const char* ParameterSettings::WEIGHING_GROUP = "WEIGHING";
//const char* ParameterSettings::WEIGHING_UNIT = "UNIT";
//const char* ParameterSettings::WEIGHING_DECI = "DECI";
//const char* ParameterSettings::WEIGHING_MAX = "MAX";
//const char* ParameterSettings::WEIGHING_DIVI = "DIVI";
//const char* ParameterSettings::WEIGHING_ZERO = "ZERO";

//const char* ParameterSettings::CALI_GROUP = "CALI";
//const char* ParameterSettings::CALI_AUTOZEROTRACE_SW = "autozerotrace_sw";
//const char* ParameterSettings::CALI_AUTOZERORANGE = "autozerorange";
//const char* ParameterSettings::CALI_BOOTAUTOZERO_SW = "bootautozero_sw";
//const char* ParameterSettings::CALI_BOOTAUTORANGE = "bootautorange";

const char* ParameterSettings::MODE_GROUP = "MODE";
const char* ParameterSettings::MODE_USEMODE = "usemode";
const char* ParameterSettings::MODE_CHECKMODE = "checkmode";
const char* ParameterSettings::MODE_CHECKEDPAUSE_SW = "checkedpause_sw";
const char* ParameterSettings::MODE_AUTOUSBCOPY = "autousbcopy";
const char* ParameterSettings::MODE_AUTOPRINT_SW = "autoprint_sw";
const char* ParameterSettings::MODE_PRINTFMT = "printfmt";

const char* ParameterSettings::COMMUNI_GROUP = "COMMUNI";
const char* ParameterSettings::COM1_GROUP = "Com1";
const char* ParameterSettings::COM2_GROUP = "Com2";
const char* ParameterSettings::BAUDRATE = "baudrate";
const char* ParameterSettings::DATABITS = "databits";
const char* ParameterSettings::STOPBITS = "stopbits";
const char* ParameterSettings::FLOWCTRL = "flowctrl";
const char* ParameterSettings::PARITY = "parity";
const char* ParameterSettings::COMMUNI_SERVER = "server";
const char* ParameterSettings::COMMUNI_PORT = "port";
const char* ParameterSettings::COMMUNI_IP = "ip";

ParameterSettings::ParameterSettings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ParameterSettings)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowState(Qt::WindowMaximized);
    //ui->tabWidget->setStyleSheet("QTabBar::tab{background-color: rgb(0, 16, 67);color:#ffffff;font:20px Ubuntu Regular;"\
                                 "border:1px solid #ffffff;max-width:120px;min-width:120px;min-height:30px;}");
    mErrTimer = new QTimer();
    mErrTimer->setSingleShot(true);
    mErrTimer->setInterval(1000);
    connect(mErrTimer,SIGNAL(timeout()),this,SLOT(onErrTimeOut()));
    connect(SPSCALEADAPTOR(),SIGNAL(setSuccess()),this,SLOT(onSetSuccess()));
    initTabWeighing();
    initTabCalibration();
    initTabMode();qDebug()<<__LINE__;
    initTabCommunication();qDebug()<<__LINE__;
    initTabSystem();qDebug()<<__LINE__;
    connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(onTabBarChanged(int)));
    ui->toolBox->setCurrentIndex(0);
    on_btn_weighing_clicked();
    lightBtn(TAB_WEIGHING);
}

ParameterSettings::~ParameterSettings()
{
    //delete mErrTimer;
    delete ui;
}

//QString ParameterSettings::getUnit()
//{
//    return getSettingValue(WEIGHING_UNIT,WEIGHING_GROUP);
//}

//QString ParameterSettings::getMax()
//{
//    return getSettingValue(WEIGHING_MAX,WEIGHING_GROUP);
//}

QVariant ParameterSettings::getSettingValue(const QString &setName, const QVariant& defltValue,const QString &groupName, const QString &fileName)
{
    TSettings set(fileName,TSettings::IniFormat);
    if(groupName!="")
    {
        set.beginGroup(groupName);
        QVariant val = set.value(setName,defltValue);
        set.endGroup();
        return val;
    }
    else
    {
        return set.value(setName,defltValue);
    }

}

void ParameterSettings::saveSettingValue(const QString &setName, const QVariant &value, const QString &groupName, const QString &fileName)
{
    TSettings set(fileName,TSettings::IniFormat);
    if(groupName!="")
    {
        set.beginGroup(groupName);
        set.setValue(setName,value);
        set.endGroup();
    }
    else
    {
        set.setValue(setName,value);
    }
}

void ParameterSettings::saveSettingValue(const QStringList &setNameList, const QVariantList &valueList, const QString &groupName, const QString &fileName)
{
    TSettings set(fileName,TSettings::IniFormat);
    if(groupName!="")
    {
        set.beginGroup(groupName);
        int i = 0;
        for(auto v:valueList)
        {
            set.setValue(setNameList.at(i),v);
            i++;
        }
        set.endGroup();
    }
    else
    {
        int i = 0;
        for(auto v:valueList)
        {
            set.setValue(setNameList.at(i),v);
            i++;
        }
    }
}

int ParameterSettings::getUseMode()
{
//    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
//    set.beginGroup(MODE_GROUP);
//    int val = set.value(MODE_USEMODE,0).toInt();
//    set.endGroup();
//    return val;
    return getSettingValue(MODE_USEMODE,0,MODE_GROUP).toInt();
}

int ParameterSettings::getCheckMode()
{
//    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
//    set.beginGroup(MODE_GROUP);
//    int val = set.value(MODE_CHECKMODE,0).toInt();
//    set.endGroup();
//    return val;
    return getSettingValue(MODE_CHECKMODE,0,MODE_GROUP).toInt();
}

int ParameterSettings::getCheckPauseMode()
{
//    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
//    set.beginGroup(MODE_GROUP);
//    int val = set.value(MODE_CHECKEDPAUSE_SW,0).toInt();
//    set.endGroup();
//    return val;
    return getSettingValue(MODE_CHECKEDPAUSE_SW,0,MODE_GROUP).toInt();
}

bool ParameterSettings::getAutoUsbCopySw()
{
//    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
//    set.beginGroup(MODE_GROUP);
//    bool val = set.value(MODE_AUTOUSBCOPY,0).toBool();
//    set.endGroup();
//    return val;
    return getSettingValue(MODE_AUTOUSBCOPY,0,MODE_GROUP).toBool();
}

void ParameterSettings::lightBtn(int index)
{
    switch (index) {
    case TAB_WEIGHING:
        ui->btn_weighing->setStyleSheet("QPushButton{background-color: rgb(218, 218, 218);color: rgb(0, 0, 0);"
                                        "border:1px solid  #ffffff}");
        ui->btn_calibrate->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                         "border:1px solid  #ffffff}");
        ui->btn_mode->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                    "border:1px solid  #ffffff}");
        ui->btn_commu->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                     "border:1px solid  #ffffff}");
        ui->btn_system->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                      "border:1px solid  #ffffff}");
        break;
    case TAB_CALIBRATE:
        ui->btn_weighing->setStyleSheet("QPushButton{background-color: rgb(0, 16,67);color: rgb(255, 255, 255);"
                                        "border:1px solid  #ffffff}");
        ui->btn_calibrate->setStyleSheet("QPushButton{background-color: rgb(218, 218, 218);color: rgb(0, 0, 0);"
                                         "border:1px solid  #ffffff}");
        ui->btn_mode->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                    "border:1px solid  #ffffff}");
        ui->btn_commu->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                     "border:1px solid  #ffffff}");
        ui->btn_system->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                      "border:1px solid  #ffffff}");
        break;
    case TAB_MODE:
        ui->btn_weighing->setStyleSheet("QPushButton{background-color: rgb(0, 16,67);color: rgb(255, 255, 255);"
                                        "border:1px solid  #ffffff}");
        ui->btn_calibrate->setStyleSheet("QPushButton{background-color: rgb(0, 16,67);color: rgb(255, 255, 255);"
                                         "border:1px solid  #ffffff}");
        ui->btn_mode->setStyleSheet("QPushButton{background-color: rgb(218, 218, 218);color: rgb(0, 0, 0);"
                                    "border:1px solid  #ffffff}");
        ui->btn_commu->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                     "border:1px solid  #ffffff}");
        ui->btn_system->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                      "border:1px solid  #ffffff}");
        break;
    case TAB_SYSTEM:
        ui->btn_weighing->setStyleSheet("QPushButton{background-color: rgb(0, 16,67);color: rgb(255, 255, 255);"
                                        "border:1px solid  #ffffff}");
        ui->btn_calibrate->setStyleSheet("QPushButton{background-color: rgb(0, 16,67);color: rgb(255, 255, 255);"
                                         "border:1px solid  #ffffff}");
        ui->btn_mode->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                    "border:1px solid  #ffffff}");
        ui->btn_commu->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                     "border:1px solid  #ffffff}");
        ui->btn_system->setStyleSheet("QPushButton{background-color: rgb(218, 218, 218);color: rgb(0, 0, 0);"
                                      "border:1px solid  #ffffff}");
        break;
    case TAB_COMMUNICATION:
        ui->btn_weighing->setStyleSheet("QPushButton{background-color: rgb(0, 16,67);color: rgb(255, 255, 255);"
                                        "border:1px solid  #ffffff}");
        ui->btn_calibrate->setStyleSheet("QPushButton{background-color: rgb(0, 16,67);color: rgb(255, 255, 255);"
                                         "border:1px solid  #ffffff}");
        ui->btn_mode->setStyleSheet("QPushButton{background-color: rgb(0, 16,67);color: rgb(255, 255, 255);"
                                    "border:1px solid  #ffffff}");
        ui->btn_commu->setStyleSheet("QPushButton{background-color: rgb(218, 218, 218);color: rgb(0, 0, 0);"
                                     "border:1px solid  #ffffff}");
        ui->btn_system->setStyleSheet("QPushButton{background-color: rgb(0, 16, 67);color: rgb(255, 255, 255);"
                                      "border:1px solid  #ffffff}");
        break;
    default:
        break;
    }
}

void ParameterSettings::on_btn_back_clicked()
{
    this->deleteLater();
}

void ParameterSettings::on_btn_weighing_clicked()
{
    ui->tabWidget->setCurrentIndex(TAB_WEIGHING);
}

void ParameterSettings::on_btn_calibrate_clicked()
{
    ui->tabWidget->setCurrentIndex(TAB_CALIBRATE);
}

void ParameterSettings::on_btn_mode_clicked()
{
    ui->tabWidget->setCurrentIndex(TAB_MODE);
}

void ParameterSettings::on_btn_commu_clicked()
{
    ui->tabWidget->setCurrentIndex(TAB_COMMUNICATION);
    lightBtn(TAB_COMMUNICATION);
}

void ParameterSettings::onTabBarChanged(int index)
{
    switch (index) {
    case 0:
        lightBtn(TAB_WEIGHING);
        break;
    case 1:
        lightBtn(TAB_CALIBRATE);
        break;
    case 2:
        lightBtn(TAB_MODE);
        break;
    case 3:
        lightBtn(TAB_SYSTEM);
        break;
    case 4:
        lightBtn(TAB_COMMUNICATION);
        break;
    default:
        break;
    }
}

void ParameterSettings::onMaxClicked()
{
    NumberKeyboard* kb = new NumberKeyboard();
    connect(kb,SIGNAL(quitWithText(QString)),this,SLOT(onSetMax(QString)));
    kb->exec();
}

bool ParameterSettings::onSetMax(QString str)
{
//    QString setCmd="#MAX#";
//    setCmd.append(str).append("\r\n");
//    SpScaleAdaptor::getInstance()->writeCmd(setCmd);
    //qDebug()<<setCmd;
    startErrTimer();
    SPSCALEADAPTOR()->setScale("#MAX#",str);//#MAX#500\r\n;
    ui->wdt_max->setShowValue(str.append("kg"/*SPSCALEADAPTOR()->getUnit())*/));

    return true;
}

bool ParameterSettings::onSetUnit(int index)
{
    startErrTimer();
//    QString setCmd = "#UNIT#";
//    setCmd.append(QString::number(index)).append("\r\n");//#UNIT#1\r\n;
//    SpScaleAdaptor::getInstance()->writeCmd(setCmd);
//    qDebug()<<setCmd;
    SPSCALEADAPTOR()->setScale("#UNIT#",QString::number(index));
    QTimer::singleShot(1000,SPSCALEADAPTOR(),SLOT(syncScalePara()));
    return true;
}

bool ParameterSettings::onSetDeci(int index)
{
    startErrTimer();
//    QString setCmd = "#DECI#";
//    setCmd.append(QString::number(index)).append("\r\n");//#DECI#1\r\n;
//    SpScaleAdaptor::getInstance()->writeCmd(setCmd);
//    qDebug()<<setCmd;
    SPSCALEADAPTOR()->setScale("#DECI#",QString::number(index));
    return true;
}

void ParameterSettings::onUpdateDeci()
{
    ui->wdt_deci->getCbx()->setCurrentIndex(SPSCALEADAPTOR()->getDeci());
}

bool ParameterSettings::onSetDivide(int index)
{
    startErrTimer();
//    QString setCmd = "#DIVI#";
//    setCmd.append(QString::number(index+1)).append("\r\n");//#DIVI#1\r\n;
//    SpScaleAdaptor::getInstance()->writeCmd(setCmd);
//    qDebug()<<setCmd;
    SPSCALEADAPTOR()->setScale("#DIVI#",QString::number(index));
    return true;
}

bool ParameterSettings::onSetZero(int index)
{
    startErrTimer();
//    QString setCmd = "#ZERO#";
//    setCmd.append(QString::number(index+1)).append("\r\n");//#ZERO#1\r\n;
//    SpScaleAdaptor::getInstance()->writeCmd(setCmd);
//    qDebug()<<setCmd;
    SPSCALEADAPTOR()->setScale("#HZERO#",QString::number(index));
    return true;
}

void ParameterSettings::onErrTimeOut()
{
    //MyMessageDialog::warning(0,"错误","通讯异常，请重试！");
    this->deleteLater();
}

void ParameterSettings::onSetSuccess()
{
    if(mErrTimer->isActive())
        mErrTimer->stop();
}

void ParameterSettings::onSetAutoZT(int index)
{
    startErrTimer();
    SPSCALEADAPTOR()->setScale("#AZEROT#",QString::number(index));
}

void ParameterSettings::onSetAutoSZR(int index)
{
    startErrTimer();
    SPSCALEADAPTOR()->setScale("#AZEROR#",QString::number(index));
}

void ParameterSettings::onUseModeChanged(int index)
{
    saveSettingValue(MODE_USEMODE,index,MODE_GROUP);
    GetMainWidget()->initUseMode();
}

void ParameterSettings::onCheckModeChanged(int index)
{
    saveSettingValue(MODE_CHECKMODE,index,MODE_GROUP);
    GetMainWidget()->initCheckMode();
}

void ParameterSettings::onCheckPauseChanged(int index)
{
    saveSettingValue(MODE_CHECKEDPAUSE_SW,index,MODE_GROUP);
}

void ParameterSettings::onAutoUsbCopyChanged(int index)
{
    saveSettingValue(MODE_AUTOUSBCOPY,index,MODE_GROUP);
}

void ParameterSettings::onSetCom1()
{
    ItemWithLabel* setLabel =static_cast<ItemWithLabel*> (this->sender());
    ComSettingDlg* comDlg = new ComSettingDlg(COM1);
    connect(comDlg,SIGNAL(quitWithText(const QString&)),setLabel,SLOT(setShowValue(const QString&)));
    comDlg->exec();
}

void ParameterSettings::onSetCom2()
{
    ItemWithLabel* setLabel =static_cast<ItemWithLabel*> (this->sender());
    ComSettingDlg* comDlg = new ComSettingDlg(COM2);
    connect(comDlg,SIGNAL(quitWithText(const QString&)),setLabel,SLOT(setShowValue(const QString&)));
    comDlg->exec();
}

void ParameterSettings::onSetServer()
{
    ItemWithLabel* setLabel =static_cast<ItemWithLabel*> (this->sender());
    ServerSetting* serverdlg = new ServerSetting();
    connect(serverdlg,SIGNAL(ServerInfoChanged(const QString&)),setLabel,SLOT(setShowValue(const QString&)));
    serverdlg->exec();
}

void ParameterSettings::onSetNetwork()
{
    ItemWithLabel* setLabel =static_cast<ItemWithLabel*> (this->sender());
    IpSetting* ipdlg = new IpSetting();
    connect(ipdlg,SIGNAL(ipChanged(const QString&)),setLabel,SLOT(setShowValue(const QString&)));
    ipdlg->exec();
}


void ParameterSettings::initTabWeighing()
{
    QLayout* layout = new QVBoxLayout;
    layout->addWidget(ui->scrollArea);

    ui->tab_wgtPa->setLayout(layout);
    QStringList autoZTList,autoSZRList;
    autoZTList<<"关"<<"0.5d"<<"1d"<<"2d"<<"3d"<<"4d"<<"5d"<<"6d"<<"7d"<<"8d";
    autoSZRList<<"关"<<"10%"<<"20%"<<"50%"<<"100%";
    ui->wdt_unit->setShowName("重量单位");
    ui->wdt_unit->addCbxItems(SPSCALEADAPTOR()->getUnitList1());
    ui->wdt_unit->getCbx()->setCurrentIndex(SPSCALEADAPTOR()->getUnitIndex());
    connect(ui->wdt_unit,SIGNAL(settingChanged(int)),this,SLOT(onSetUnit(int)));

    ui->wdt_deci->setShowName("小数点位数[kg]");
    ui->wdt_deci->addCbxItems(SPSCALEADAPTOR()->getDeciList());
    ui->wdt_deci->getCbx()->setCurrentIndex(SPSCALEADAPTOR()->getDeci());
    connect(ui->wdt_deci,SIGNAL(settingChanged(int)),this,SLOT(onSetDeci(int)));
    connect(SPSCALEADAPTOR(),SIGNAL(updateDeci()),this,SLOT(onUpdateDeci()));

    ui->wdt_max->setShowName("最大称量");

    ui->wdt_max->setShowValue(SPSCALEADAPTOR()->getMaxkgStr()+"kg"/*SPSCALEADAPTOR()->getUnit()*/);
    connect(ui->wdt_max,SIGNAL(clicked()),this,SLOT(onMaxClicked()));

    ui->wdt_divide->setShowName("分度值");
    ui->wdt_divide->addCbxItems(SPSCALEADAPTOR()->getDiviList());qDebug()<<"divi"<<SPSCALEADAPTOR()->getDiviIndex();
    ui->wdt_divide->getCbx()->setCurrentIndex(SPSCALEADAPTOR()->getDiviIndex());
    connect(ui->wdt_divide,SIGNAL(settingChanged(int)),this,SLOT(onSetDivide(int)));

    ui->wdt_hzero->setShowName("手动置零范围");
    ui->wdt_hzero->addCbxItems(SPSCALEADAPTOR()->getHZeroList());
    ui->wdt_hzero->getCbx()->setCurrentIndex(SPSCALEADAPTOR()->getHZero());
    connect(ui->wdt_hzero,SIGNAL(settingChanged(int)),this,SLOT(onSetZero(int)));

    ui->wdt_zeroTrace->setShowName("自动零点追踪");
    ui->wdt_zeroTrace->addCbxItems(autoZTList);
    ui->wdt_zeroTrace->getCbx()->setCurrentIndex(SPSCALEADAPTOR()->getAutoZeroTrace());
    connect(ui->wdt_zeroTrace,SIGNAL(settingChanged(int)),this,SLOT(onSetAutoZT(int)));

    ui->wdt_startZeroRange->setShowName("上电自动置零");
    ui->wdt_startZeroRange->addCbxItems(autoSZRList);
    ui->wdt_startZeroRange->getCbx()->setCurrentIndex(SPSCALEADAPTOR()->getAutoZeroRange());
    connect(ui->wdt_startZeroRange,SIGNAL(settingChanged(int)),this,SLOT(onSetAutoSZR(int)));     
}

void ParameterSettings::initTabCalibration()
{

//    QLayout* layout = new QVBoxLayout;
//    layout->addWidget(ui->scrollArea_2);
//    ui->tab_calPa->setLayout(layout);
//    QStringList autoZeroTraceSw,autoZeroTraceRange,bootZeroSw,bootZeroRange;
//    autoZeroTraceSw<<"允许"<<"禁止";
//    autoZeroTraceRange<<"0.5d"<<"1d"<<"2d"<<"3d"<<"4d";
//    bootZeroSw<<"开"<<"关";
//    bootZeroRange<<"50%"<<"100%";

//    ui->wdt_zeroTraceSw->setShowName("自动零点追踪开关");
//    ui->wdt_zeroTraceSw->addCbxItems(autoZeroTraceSw);

//    ui->wdt_zeroTraceRange->setShowName("自动零点追踪范围");
//    ui->wdt_zeroTraceRange->addCbxItems(autoZeroTraceRange);

//    ui->wdt_bootZeroSw->setShowName("上电自动置零开关");
//    ui->wdt_bootZeroSw->addCbxItems(bootZeroSw);

//    ui->wdt_bootZeroRange->setShowName("上电自动置零范围");
//    ui->wdt_bootZeroRange->addCbxItems(bootZeroRange);

//    ui->wdt_null->setShowName("");
//    ui->wdt_null->setShowValue("");
}

void ParameterSettings::initTabMode()
{
    QLayout* layout = new QVBoxLayout;
    layout->addWidget(ui->scrollArea_3);
    ui->tab_modePa->setLayout(layout);

    QStringList useModeList,checkModeList,checkPauseList,AutoUsbCopyList,autoPrintSw,printFmtList;
    useModeList<<"次品剔除模式[下限/合格/上限]"<<"重量分区模式";
    checkModeList<<"稳定检重模式"/*<<"静态检重模式"*/<<"动态检重模式";
    checkPauseList<<"不暂停"<<"暂停"<<"次品时暂停";
    AutoUsbCopyList<<"禁止"<<"允许";
    autoPrintSw<<"关"<<"开";
    printFmtList<<"无";;
    ui->wdt_useMode->setShowName("应用模式");
    ui->wdt_useMode->addCbxItems(useModeList);
    ui->wdt_useMode->getCbx()->setCurrentIndex(getUseMode());
    connect(ui->wdt_useMode,SIGNAL(settingChanged(int)),this,SLOT(onUseModeChanged(int)));

    ui->wdt_checkMode->setShowName("检重模式");
    ui->wdt_checkMode->addCbxItems(checkModeList);
    ui->wdt_checkMode->getCbx()->setCurrentIndex(getCheckMode());
    connect(ui->wdt_checkMode,SIGNAL(settingChanged(int)),this,SLOT(onCheckModeChanged(int)));

    ui->wdt_checkPause->setShowName("检重后暂停");
    ui->wdt_checkPause->addCbxItems(checkPauseList);
    ui->wdt_checkPause->getCbx()->setCurrentIndex(getCheckPauseMode());
    connect(ui->wdt_checkPause,SIGNAL(settingChanged(int)),this,SLOT(onCheckPauseChanged(int)));

    ui->wdt_AutoUsbCopy->setShowName("自动U盘拷贝");
    ui->wdt_AutoUsbCopy->addCbxItems(AutoUsbCopyList);
    ui->wdt_AutoUsbCopy->getCbx()->setCurrentIndex(getAutoUsbCopySw());
    connect(ui->wdt_AutoUsbCopy,SIGNAL(settingChanged(int)),this,SLOT(onAutoUsbCopyChanged(int)));

    ui->wdt_autoPrint->setShowName("自动打印");
    ui->wdt_autoPrint->addCbxItems(autoPrintSw);

    ui->wdt_printFmt->setShowName("打印格式");
    ui->wdt_printFmt->addCbxItems(printFmtList);


//    ui->wdt_modeNull->setShowName("");
//    ui->wdt_modeNull->setShowValue("");
}

void ParameterSettings::initTabCommunication()
{
    QLayout* layout = new QVBoxLayout;
    layout->addWidget(ui->scrollArea_4);
    ui->tab_comPa->setLayout(layout);



    ui->wdt_com1Setting->setShowName("Com1[RS232]");
    QString com1ShowValue =QString("%1,%2,%3,%4,%5").arg(COM1->getBaudRateStr()).arg(COM1->getDataBitsStr())
                                .arg(COM1->getParityStr()).arg(COM1->getFlowCtrlStr()).arg(COM1->getStopBitsStr());
    ui->wdt_com1Setting->setShowValue(com1ShowValue);
    connect(ui->wdt_com1Setting,SIGNAL(clicked()),this,SLOT(onSetCom1()));

    ui->wdt_com2Setting->setShowName("Com2[RS485]");
    QString com2ShowValue =QString("%1,%2,%3,%4,%5").arg(COM2->getBaudRateStr()).arg(COM2->getDataBitsStr())
                                .arg(COM2->getParityStr()).arg(COM2->getFlowCtrlStr()).arg(COM2->getStopBitsStr());
    ui->wdt_com2Setting->setShowValue(com2ShowValue);
    connect(ui->wdt_com2Setting,SIGNAL(clicked()),this,SLOT(onSetCom2()));

    ui->wdt_serverSetting->setShowName("服务器设置");
    QString serverip = getSettingValue(COMMUNI_SERVER,"192.168.1.100",COMMUNI_GROUP).toString();
    QString port = getSettingValue(COMMUNI_PORT,"12306",COMMUNI_GROUP).toString();
    ui->wdt_serverSetting->setShowValue("IP  :"+serverip+"\nPort:"+port);
    connect(ui->wdt_serverSetting,SIGNAL(clicked()),this,SLOT(onSetServer()));

    ui->wdt_network->setShowName("IP地址设置");
    QString ip,mask,gateway;
    platform_sys_info_c::get_sys_ip_addr(ip,mask,gateway);
    ui->wdt_network->setShowValue("IP  :"+ip+"\nMask:"+mask+"\n网关:"+gateway);
    connect(ui->wdt_network,SIGNAL(clicked()),this,SLOT(onSetNetwork()));
}

void ParameterSettings::initTabSystem()
{
    int ava_memory,total_memory;
    quint64 ava_disk,total_disk;
    platform_sys_info_c::get_memory_info(ava_memory,total_memory);
    platform_sys_info_c::get_disk_info(ava_disk,total_disk);
    ui->ava_memory->setText(QString::number(ava_memory/1024).append("M"));
    ui->total_memory->setText(QString::number(total_memory/1024).append("M"));
    ui->ava_disk->setText(QString::number(ava_disk/(1024*1024)).append("M"));
    ui->total_disk->setText(QString::number(total_disk/(1024*1024)).append("M"));
    qDebug()<<platform_sys_info_c::get_rtc_time().date();
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    ui->chk_mousecursor->setChecked(QWSServer::isCursorVisible());
    ui->dateTimeEdit->setSelectedSection(QDateTimeEdit::YearSection);
}

void ParameterSettings::startErrTimer()
{
    if(mErrTimer->isActive()) mErrTimer->stop();
    mErrTimer->start();
}

void ParameterSettings::on_btn_system_clicked()
{
    ui->tabWidget->setCurrentIndex(TAB_SYSTEM);
    lightBtn(TAB_SYSTEM);
}

void ParameterSettings::on_btn_calibrate_s_clicked()
{
    if(QApplication::desktop()->height() <= 600)
    {
        touch_calibration dlg2(this);
        dlg2.calibration_exec();

    }
    else
    {
        touch_calibration_nine dlg(this);
        dlg.calibration_exec();
    }
}

void ParameterSettings::on_backlightSld_valueChanged(int value)
{
    qDebug()<<hal_display_get_backlight_brightness();
    qDebug()<<value;
   qDebug()<<"return value:"<<hal_display_set_backlight_brightness(value);
}


void ParameterSettings::on_btn_setDatetime_clicked()
{
    QDateTime datetime = ui->dateTimeEdit->dateTime();
    qDebug()<<platform_sys_info_c::set_rtc_time(datetime);
    QString datetimeStr = datetime.toString("yyyy-MM-dd hh:mm:ss");

    QString dateStr = " date -s \"" + datetimeStr + "\"";
    DoSystem(dateStr.toLatin1().data());

    DoSystem("hwclock -w");
    qDebug()<<QDateTime::currentDateTime();
}


void ParameterSettings::on_btn_next_clicked()
{
    QDateTimeEdit * edit = ui->dateTimeEdit;
    edit->setFocus();
    QDateTimeEdit::Section section = edit->currentSection();
    switch (section) {
    case QDateTimeEdit::YearSection:
        edit->setSelectedSection(QDateTimeEdit::MonthSection);
        break;
    case QDateTimeEdit::MonthSection:
        edit->setSelectedSection(QDateTimeEdit::DaySection);
        break;
    case QDateTimeEdit::DaySection:
        edit->setSelectedSection(QDateTimeEdit::HourSection);
        break;
    case QDateTimeEdit::HourSection:
        edit->setSelectedSection(QDateTimeEdit::MinuteSection);
        break;
    case QDateTimeEdit::MinuteSection:
        edit->setSelectedSection(QDateTimeEdit::SecondSection);
        break;
    case QDateTimeEdit::SecondSection:
        edit->setSelectedSection(QDateTimeEdit::YearSection);
        break;
    default:
        edit->setSelectedSection(QDateTimeEdit::YearSection);
        break;
    }
}


void ParameterSettings::on_chk_mousecursor_clicked()
{
    QWSServer::setCursorVisible(ui->chk_mousecursor->isChecked());
}

